package net.lovesrc.support.service.geetest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhouzhou.fastjava.http.MyHttpRequest;
import com.zhouzhou.fastjava.http.Requests;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;

/**
 * 滑块验证码识别
 */
public class GeetestService {

    private static final String USERNAME = "qiang";

    private static final String PASSWORD = "abcd4321";

    private static final RequestConfig config = RequestConfig.custom()
            .setCookieSpec(CookieSpecs.STANDARD)
            .setSocketTimeout(60 * 1000)
            .setConnectTimeout(60 * 1000)
            .setConnectionRequestTimeout(60 * 1000)
            .build();

    public GeetestResult shibie(
            String gt,
            String challenge,
            String referer,
            int model
    ) {

        CloseableHttpClient httpClient = Requests.newHttpClientBuilder(new BasicCookieStore())
                .setDefaultRequestConfig(config)
                .build();
        JSONObject resJson = MyHttpRequest.get(httpClient, "http://jiyanapi.c2567.com/shibie")
                .param("user", USERNAME)
                .param("pass", PASSWORD)
                .param("gt", gt)
                .param("challenge", challenge)
                .param("referer", referer)
                .param("model", model)
                .param("return", "json")
                .param("format", "utf-8")
                .execute()
                .asJson();
        return JSON.toJavaObject(resJson, GeetestResult.class);
    }

}
